import prettier from 'prettier/standalone.js'
import parserBabel from 'prettier/parser-babel.js'
import parserHtml from 'prettier/parser-html.js'
import parserCss from 'prettier/parser-postcss.js'

// 格式化代码，默认为js
export default function formatCode(code, type) {
  try {
    let parser, plugins
    if (type === 'vue') {
      parser = 'vue'
      plugins = [parserHtml, parserBabel, parserCss]
    } else {
      parser = 'babel'
      plugins = [parserBabel]
    }
    const result = prettier.format(code, {
      semi: false,
      parser,
      plugins,
    })
    return result
  } catch (error) {
    console.error(error)
    return code
  }
}
